const mysql = require('../db/mysql57');

const activityDao = {
    getActivityList(uid, cursor, limit) {
        let sql;
        let data;
        if (cursor) {
            sql = `select
                    a.id as id,
                    a.title as title,
                    a.imgId as imgId,
                    DATE_FORMAT(a.createTime, '%Y-%m-%d %H:%i:%s') as date,
                    a.activityTime as duration,
                    a.address as location,
                    (select count(1) from ActivityJoin where activityId = a.id) as wantToGoCount
                from Activity as a
                where a.id <= ?
                order by a.id desc
                limit ?`;
            data = [ cursor, limit + 1 ];
        } else {
            sql = `select
                    a.id as id,
                    a.title as title,
                    a.imgId as imgId,
                    DATE_FORMAT(a.createTime, '%Y-%m-%d %H:%i:%s') as date,
                    a.activityTime as duration,
                    a.address as location,
                    (select count(1) from ActivityJoin where activityId = a.id) as wantToGoCount
                from Activity as a
                order by a.id desc
                limit ?`;
            data = [ limit + 1 ];
        }
        return mysql.sqlExec(sql, data);
    },
    joinActivity(uid, activityId) {
        let sql = `insert into ActivityJoin (uid, activityId) (
                select ?, ? where NOT EXISTS (SELECT 1 FROM ActivityJoin WHERE uid = ? AND activityId = ?)
            )`;
        let data = [ uid, activityId, uid, activityId ];
        return mysql.sqlExec(sql, data);
    },
    cancelActivity(uid, activityId) {
        let sql = `DELETE FROM ActivityJoin WHERE uid = ? AND activityId = ?`;
        let data = [ uid, activityId ];
        return mysql.sqlExec(sql, data);
    },
    getActivityInfo(uid, activityId) {
        let sql = `select
                a.id as id,
                a.title as title,
                a.content as content,
                a.imgId as imgId,
                DATE_FORMAT(a.createTime, '%Y-%m-%d %H:%i:%s') as date,
                a.activityTime as duration,
                a.address as location,
                (select count(1) from ActivityJoin where activityId = a.id) as wantToGoCount,
                (select count(1) from ActivityJoin where activityId = a.id and uid = ?) as isJoin
            from Activity as a
            where a.id = ?`;
        let data = [ uid, activityId ];
        return mysql.sqlExec(sql, data);
    },
    getJoinedActivityList(uid, cursor, limit) {
        let sql;
        let data;
        if (cursor) {
            sql = `select
                    aj.id as id,
                    a.id as activityId,
                    a.title as title,
                    a.imgId as imgId,
                    DATE_FORMAT(a.createTime, '%Y-%m-%d %H:%i:%s') as date,
                    a.activityTime as duration,
                    a.address as location,
                    (select count(1) from ActivityJoin where activityId = a.id) as wantToGoCount
                from ActivityJoin as aj
                left join Activity as a on a.id = aj.activityId
                where aj.uid = ? and aj.id <= ?
                order by aj.id desc
                limit ?`;
            data = [ uid, cursor, limit + 1 ];
        } else {
            sql = `select
                    aj.id as id,
                    a.id as activityId,
                    a.title as title,
                    a.imgId as imgId,
                    DATE_FORMAT(a.createTime, '%Y-%m-%d %H:%i:%s') as date,
                    a.activityTime as duration,
                    a.address as location,
                    (select count(1) from ActivityJoin where activityId = a.id) as wantToGoCount
                from ActivityJoin as aj
                left join Activity as a on a.id = aj.activityId
                where aj.uid = ?
                order by aj.id desc
                limit ?`;
            data = [ uid, limit + 1 ];
        }
        return mysql.sqlExec(sql, data);
    }
};

module.exports = activityDao;